﻿using System;
using System.Web.Mvc;

namespace Ams.Mvc.Bootstrap
{
    public enum Btn
    {
        Default,
        Primary,
        Success,
        Info,
        Warning,
        Danger,
        Link
    }

    public static class BootstrapButtonExtensions
    {
        #region Button()
        public static MvcHtmlString Button<TModel>(this BootstrapHelper<TModel> helper, string buttonText)
        {
            return Button(helper, buttonText, Btn.Default, null);
        }
        public static MvcHtmlString Button<TModel>(this BootstrapHelper<TModel> helper, string buttonText, Btn buttonType)
        {
            return Button(helper, buttonText, buttonType, null);
        }
        public static MvcHtmlString Button<TModel>(this BootstrapHelper<TModel> helper, string buttonText, object htmlAttributes)
        {
            return Button(helper, buttonText, Btn.Default, htmlAttributes);
        }
        public static MvcHtmlString Button<TModel>(this BootstrapHelper<TModel> helper, string buttonText, Btn buttonType, object htmlAttributes)
        {
            var tagBuilder = new TagBuilder("button");
            tagBuilder.SetInnerText(buttonText);
            tagBuilder.MergeAttribute("type","button");
            tagBuilder.AddCssClass("btn");
            tagBuilder.AddCssClass("btn-" + Enum.GetName(typeof(Btn), buttonType).ToLower());
            tagBuilder.MergeAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes), true);
            return tagBuilder.ToMvcHtmlString();
        } 
        #endregion

        #region SubmitButton()
        public static MvcHtmlString SubmitButton<TModel>(this BootstrapHelper<TModel> helper, string buttonText)
        {
            return SubmitButton(helper, buttonText, Btn.Primary, null);
        }
        public static MvcHtmlString SubmitButton<TModel>(this BootstrapHelper<TModel> helper, string buttonText, Btn buttonType)
        {
            return SubmitButton(helper, buttonText, buttonType, null);
        }
        public static MvcHtmlString SubmitButton<TModel>(this BootstrapHelper<TModel> helper, string buttonText, object htmlAttributes)
        {
            return SubmitButton(helper, buttonText, Btn.Primary, htmlAttributes);
        }
        public static MvcHtmlString SubmitButton<TModel>(this BootstrapHelper<TModel> helper, string buttonText, Btn buttonType, object htmlAttributes)
        {
            var tagBuilder = new TagBuilder("button");
            tagBuilder.MergeAttribute("type", "submit");
            tagBuilder.SetInnerText(buttonText);
            tagBuilder.AddCssClass("btn");
            tagBuilder.AddCssClass("btn-" + Enum.GetName(typeof(Btn), buttonType).ToLower());
            tagBuilder.MergeAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
            var html = tagBuilder.ToString();

            if (helper.Form != null && helper.Form.FormType == Form.Horizontal)
            {
                var divWrapper = new TagBuilder("div");
                divWrapper.AddCssClass("col-sm-offset-2 col-sm-10");
                divWrapper.InnerHtml = html;
                
                var formGroup = new TagBuilder("div");
                formGroup.AddCssClass("form-group");
                formGroup.InnerHtml = divWrapper.ToString();
                
                html = formGroup.ToString();
            }

            return new MvcHtmlString(html);
        } 
        #endregion

        #region ActionButton()
        public static MvcHtmlString ActionButton<TModel>(this BootstrapHelper<TModel> helper, string buttonText, string actionName)
        {
            var currentController = helper.ViewContext.RouteData.GetRequiredString("controller");
            return ActionButton(helper, buttonText, actionName, currentController, null, Btn.Default, null);
        }
        public static MvcHtmlString ActionButton<TModel>(this BootstrapHelper<TModel> helper, string buttonText, string actionName, string controllerName)
        {
            return ActionButton(helper, buttonText, actionName, controllerName,null, Btn.Default, null);
        }
        public static MvcHtmlString ActionButton<TModel>(this BootstrapHelper<TModel> helper, string buttonText, string actionName, string controllerName, object routeValues)
        {
            return ActionButton(helper, buttonText, actionName, controllerName, routeValues, Btn.Default, null);
        }
        public static MvcHtmlString ActionButton<TModel>(this BootstrapHelper<TModel> helper, string buttonText, string actionName, Btn buttonType)
        {
            var currentController = helper.ViewContext.RouteData.GetRequiredString("controller");
            return ActionButton(helper, buttonText, actionName, currentController, buttonType, null);
        }
        public static MvcHtmlString ActionButton<TModel>(this BootstrapHelper<TModel> helper, string buttonText, string actionName, string controllerName, Btn buttonType)
        {
            return ActionButton(helper, buttonText, actionName, controllerName, buttonType, null);
        }
        public static MvcHtmlString ActionButton<TModel>(this BootstrapHelper<TModel> helper, string buttonText, string actionName, string controllerName, object routeValues, Btn buttonType)
        {
            return ActionButton(helper, buttonText, actionName, controllerName, routeValues, buttonType, null);
        }
        public static MvcHtmlString ActionButton<TModel>(this BootstrapHelper<TModel> helper, string buttonText, string actionName, object htmlAttributes)
        {
            var currentController = helper.ViewContext.RouteData.GetRequiredString("controller");
            return ActionButton(helper, buttonText, actionName, currentController, Btn.Default, null);
        }
        public static MvcHtmlString ActionButton<TModel>(this BootstrapHelper<TModel> helper, string buttonText, string actionName, object routeValues, object htmlAttributes)
        {
            var currentController = helper.ViewContext.RouteData.GetRequiredString("controller");
            return ActionButton(helper, buttonText, actionName, currentController, routeValues, Btn.Default, null);
        }
        public static MvcHtmlString ActionButton<TModel>(this BootstrapHelper<TModel> helper, string buttonText, string actionName, string controllerName, object routeValues, object htmlAttributes)
        {
            return ActionButton(helper, buttonText, actionName, controllerName, routeValues, Btn.Default, htmlAttributes);
        }
        public static MvcHtmlString ActionButton<TModel>(this BootstrapHelper<TModel> helper, string buttonText, string actionName, string controllerName, object routeValues, Btn buttonType, object htmlAttributes)
        {
            var urlHelper = new UrlHelper(helper.ViewContext.HttpContext.Request.RequestContext);

            var tagBuilder = new TagBuilder("a");
            tagBuilder.SetInnerText(buttonText);
            tagBuilder.MergeAttribute("href", urlHelper.Action(actionName, controllerName, routeValues));
            tagBuilder.AddCssClass("btn");
            tagBuilder.AddCssClass("btn-" + Enum.GetName(typeof(Btn), buttonType).ToLower());
            tagBuilder.MergeAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes), true);
            return tagBuilder.ToMvcHtmlString();
        }
        #endregion

        #region CloseButton()
        public static MvcHtmlString CloseButton<TModel>(this BootstrapHelper<TModel> helper)
        {
            return CloseButton(helper, null);
        }
        public static MvcHtmlString CloseButton<TModel>(this BootstrapHelper<TModel> helper, object htmlAttributes)
        {
            var tagBuilder = new TagBuilder("button");
            tagBuilder.InnerHtml = "&times;";
            tagBuilder.MergeAttribute("type", "button");
            tagBuilder.MergeAttribute("aria-hidden", "true");
            tagBuilder.AddCssClass("close");
            tagBuilder.MergeAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes), true);
            return tagBuilder.ToMvcHtmlString();
        }
        #endregion
    }
}
